package DTO;


import lombok.Data;
import lombok.Setter;
import someTestExcemple.reflect.annotation.MyAnnotation1;

@Data
@MyAnnotation1(name = "Jan", age = 33,sex="女")
public class Student implements Cloneable,Comparable<Student> {
    @Setter
    private String name; //在的Student类中，name是String类型，id是Integer类型，都是不可变对象。
    private Integer id;
    @Setter
    public Address address;

    public Student() {
    }


    //指定比较规则 统一约定this和o比较：大于 o 返回正数，小于o返回负数，等于o返回0
    @Override
    public int compareTo(Student o) {
        //按id排序
//        if(this.id.compareTo(o.id) > 0) {
//            return 1;
//        } else if (this.id.compareTo(o.id) < 0){
//            return -1;
//        } else {
//            return 0;
//        }
        //按id升序
        return this.id - o.id;
    }

    public Student(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    public Student(String name, Integer id, Address address) {
        this.name = name;
        this.id = id;
        this.address = address;
    }

    protected Student clone() throws CloneNotSupportedException {
        return (Student) super.clone(); //浅拷贝 -基本类型字段复制值，引用类型的字段复制引用
    }
    protected Student deepClone() throws CloneNotSupportedException {
        Student studentCopy = (Student) super.clone();
        studentCopy.address = (Address) address.clone();
        return studentCopy;
    }

//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", id='" + id + '\'' +
//                '}';
//    }

    public static void main(String[] args) {
        Integer a = 100;
        Integer b = 100;
        //包装类的缓存机制 同一数值的包装类对象可能是同一实例
        System.out.println(a == b);
        Student student = new Student("tom",12);
        student.setAddress(new Address("中国","江苏","南京"));
        try {
            Student student1 = student.clone();
            System.out.println(student);
            System.out.println((student1.hashCode()));
            System.out.println(student.getClass());
            //String不可变，回生成新的对象
            student1.setName("jack");
            student1.setId(435);
            System.out.println(student.equals(student1));
            //student1.setAddress(student.getAddress().clone());
            System.out.println(student1);
            //由于Student的Name和id类型是不可变的，因此修改student1的name不会影响student的name。
            System.out.println(student);
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    public void setId(int id) {
        this.id = id;
    }

}
